package com.ums.upos;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.LargeTest;
import android.support.test.rule.ServiceTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.util.Log;

import com.ums.upos.service.DeviceServiceEngineService;
import com.ums.upos.uapi.device.logrecorder.LogRecorder;
import com.ums.upos.uapi.engine.DeviceServiceEngine;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Created by Administrator on 2017/3/31.
 */

@RunWith(AndroidJUnit4.class)
@LargeTest
public class DeviceServiceTest {
    private DeviceServiceEngine mDeviceHandle = null;

    @Rule
    public final  ServiceTestRule mServiceRule = new ServiceTestRule();

    @AfterClass
    public static void afterall() throws InterruptedException {

    }


    ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            mDeviceHandle = DeviceServiceEngine.Stub.asInterface(iBinder);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            mDeviceHandle = null;
        }
    };

    @Before
    public void start() {

            Intent intent = new Intent(InstrumentationRegistry.getTargetContext(), DeviceServiceEngineService.class);

            try {
                mServiceRule.bindService(intent, conn, Context.BIND_ABOVE_CLIENT);
                Bundle loginBundle = new Bundle();
                loginBundle.putInt("workMode", 0);
                mDeviceHandle.login(loginBundle, "00000001");
                Thread.sleep(5 * 1000, 0);
            } catch (Exception e) {
                mDeviceHandle = null;
                e.printStackTrace();
            }

    }

    @Test
    public void test_getDeviceDetail() {
        try {

            LogRecorder logRecorder = mDeviceHandle.getLogRecorder();
            int log_path = logRecorder.getStatus();
            Log.d("DeviceServiceTest", "log_path: " + log_path);
            Log.i("DeviceServiceTest", "log_path: " + log_path);
        } catch (Exception e) {

            Log.i("DeviceServiceTest", "log_path----------- ");
            e.printStackTrace();
        }
    }

}
